package by.grsu.kononovich.page.simple;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;

import by.grsu.kononovich.app.MyFirstWebApplication;
import by.grsu.kononovich.dao.impl.AutoDao;
import by.grsu.kononovich.entity.Auto;

public class AutoEditPage extends WebPage{
	public AutoEditPage(final Auto auto){
		super();
		
		final Form<Auto> form = new Form<Auto>("form", new CompoundPropertyModel<Auto>(auto));
		add(form);
		
		form.add(new TextField<String>("Mark"));
		form.add(new TextField<String>("Model"));
		form.add(new TextField<Boolean>("Freedom"));
		form.add(new TextField<Integer>("Cost"));
		
		form.add(new SubmitLink("Save"){
			@Override
			public void onSubmit(){
				super.onSubmit();
				
				final AutoDao autoDao = new AutoDao(MyFirstWebApplication.DATA_XML_FOLDER);
				
				if (auto.getId() == null){
					autoDao.saveNew(auto);
				} else {
					autoDao.update(auto);
				}
				
				setResponsePage(new AutosListPage());
				System.out.println("button Save pressed");
			}
		});
		
		form.add(new Link("Cancel"){
			@Override
			public void onClick(){
				setResponsePage(new AutosListPage());
				System.out.println("button Cancel pressed");
			}
		});
	}

}
